var host = getApp().globalData.host;
var util = require('../../utils/util.js');
var apiObject = require('../../utils/api.js');
var app=getApp();


Page({

  data: {
    employeeNo: "",
    name: "",
    gender: 1,
    phone: "",
    email: "",
    password: "",
    repassword: "",
    vcode:"",
    image: "",
    vcodeText: "获取验证码",
    isChangeImage: false,
    headUrl: "/image/add.png"
  },

  onLoad: function (e) {
  },

  uploadImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: function (res) {
        that.setData({
          isChangeImage: true,
          headUrl: res.tempFilePaths[0]
        })
      }
    })
  },

  chooseMale: function (e) {
    this.setData({
      gender: 1
    })
  },
  chooseFemale: function (e) {
    this.setData({
      gender: 0
    })
  },

  employeeNoInput: function (e) {
    this.setData({
      employeeNo: e.detail.value
    })
  },

  nameInput:function(e){
    this.setData({
      name:e.detail.value
    })
  },

  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  emailInput: function (e) {
    this.setData({
      email: e.detail.value
    })
  },

  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  repasswordInput: function (e) {
    this.setData({
      repassword: e.detail.value
    })
  },

  vcodeInput: function (e) {
    this.setData({
      vcode: e.detail.value
    })
  },


  sendCode: function () { //发送验证码
    if (this.data.phone.trim() == "") {
      util.showModal("手机号不能为空");
      return;
    }
    if (!/^1[34578]\d{9}$/.test(this.data.phone)) {
      util.showModal("请输入正确的手机号");
      return;
    }
    var num = 60,
      that = this,
      timer;
    if (this.data.vcodeBtnDisabled) return;

    wx.request({
      url: apiObject.api.getVerificationCode,
      data:{
        phone: that.data.phone
      },
      header: {
        'Content-Type': "application/x-www-form-urlencoded",
      },
      success: function (res) {
        res=res.data;
        if (res.code==200){
          console.log("验证码发送成功:", res.result.vcode);
        } else {
          clearInterval(timer);
          that.setData({
            vcodeBtnDisabled: false,
            vcodeText: '获取验证码'
          });
          util.showModal(res.message);
        }
      }
    })


    this.setData({
      vcodeBtnDisabled: true,
      vcodeText: num + 's'
    });
    timer = setInterval(function () {
      num--;
      that.setData({
        vcodeBtnDisabled: true,
        vcodeText: num + 's'
      });
      if (num == 0) {
        clearInterval(timer);
        that.setData({
          vcodeBtnDisabled: false,
          vcodeText: '获取验证码'
        });
      }
    }, 1000);

  },


  submit: function () {
    var that = this;
    if (this.data.employeeNo.trim() == "") {
      util.showModal("工号不能为空");
      return;
    }
    if (this.data.name.trim() == "") {
      util.showModal("姓名不能为空");
      return;
    }
    if (this.data.phone.trim() == "") {
      util.showModal("手机号不能为空");
      return;
    }
    if (!/^1[34578]\d{9}$/.test(this.data.phone)) {
      util.showModal("请输入正确的手机号");
      return;
    }
    // if (this.data.vcode.trim() == "") {
    //   util.showModal("验证码不能为空");
    //   return;
    // }
    if (this.data.email.trim() == "") {
      util.showModal("邮箱不能为空");
      return;
    }
    if (!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(this.data.email)) {
      util.showModal("请输入正确的邮箱");
      return;
    }
    if (this.data.password.trim() == "") {
      util.showModal("密码不能为空");
      return;
    }
    if (this.data.password != this.data.repassword) {
      util.showModal("两次输入密码必须相同");
      return;
    }
    if (!this.data.isChangeImage) {
      util.showModal("必须上传照片");
      return;
    }


    wx.uploadFile({
      url: apiObject.api.register,
      filePath: that.data.headUrl,
      name: "image",
      formData: {
        empno: that.data.employeeNo,
        name: that.data.name,
        sex: that.data.gender,
        phone: that.data.phone,
        vcode: that.data.vcode,
        email: that.data.email,
        password: that.data.password,
      },
      header: {
        'Content-Type': "multipart/form-data"
      },
      success: function (res) {
        res = JSON.parse(res.data);
        console.log(res)
        if (res.code == 200) {
          util.showModal("申请成功,请等待管理员同意").then(function () {
            wx.navigateBack()
          })
        } else {
          util.showModal(res.message);
        }
      }
    })



  }

})